<template>
  <div class="app-container calendar-list-container">
    <el-tabs type="border-card">
      <el-tab-pane label="业务">
        <business ref="business" :business-data="businessData"/>
      </el-tab-pane>
      <el-tab-pane label="系统">
        <system ref="system" :system-data="systemData"/>
      </el-tab-pane>
      <el-tab-pane label="基础设置">
        <baseSet ref="baseSet" :system-data="systemData"/>
      </el-tab-pane>
      <el-tab-pane label="注册与安全">
        <registerAndSafety ref="registerAndSafety" :system-data="systemData"/>
      </el-tab-pane>
    </el-tabs>

    <el-row style="margin-top: 60px; padding-left: 60px;">
      <el-button type="primary" @click="submit">确认</el-button>
    </el-row>
  </div>
</template>

<script>
import system from './tabs/system'
import business from './tabs/business'
import baseSet from './tabs/baseSet'
import registerAndSafety from './tabs/registerAndSafety'

import { codeValidation } from '@/methods/codeValidation'

import { systemConfigView, systemConfigUpdate } from '@/api/system'

export default {
  name: 'Index',
  components: {
    system,
    baseSet,
    registerAndSafety,
    business
  },
  data() {
    return {
      // 系统数据
      systemData: {},
      businessData: {}
    }
  },
  created() {
    this.systemConfigView()
  },
  methods: {

    systemConfigView() {
      var self = this
      systemConfigView().then(response => {
        self.systemData = response.data.system
        self.businessData = response.data.business
      })
    },

    // 点击提交时 先去每个tab 里面调用验证方法 , 会返回验证结果
    // 如果都为 true 的时候则调用修改接口
    submit() {
      var isSystem = this.$refs.system.verifyData()
      var isBaseSet = this.$refs.baseSet.verifyData()
      var isRegisterAndSafety = this.$refs.registerAndSafety.verifyData()
      var isBusiness = this.$refs.business.verifyData()
      if (isSystem && isBaseSet && isRegisterAndSafety && isBusiness) {
        this._systemConfigUpdate()
      } else {
        var self = this
        var code = '1'
        var content = '有必填项未填写'
        codeValidation(code, content, self)
      }
    },

    // 修改接口
    _systemConfigUpdate() {
      var self = this
      var system = this.$refs.system.returnData()
      var baseSet = this.$refs.baseSet.returnData()
      var registerAndSafety = this.$refs.registerAndSafety.returnData()
      var business = this.$refs.business.returnData()

      var temp = {}
      Object.assign(temp, system, baseSet, registerAndSafety, business)

      var data = {}
      for (var key in temp) {
        data[`key_${key}`] = temp[key]
      }
      systemConfigUpdate(data).then(response => {
        var code = '0'
        var content = response.data.content
        codeValidation(code, content, self)
      })
    }

  }
}
</script>

<style scoped>

</style>
